package ru.belokuriha_online.resort_hotels_belokuriha;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;

import android.net.Uri;
import android.os.Bundle;
import android.os.Build;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Aboutapp extends Activity {
	int FlagOnlain;
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aboutapp);
		try {
		FlagOnlain = getIntent().getExtras().getInt("FlagOnlain");
		try {
			TextView TVversionapp =(TextView) findViewById(R.id.aboutapptextView2);
			TVversionapp.setText(getString(R.string.version_app_t)+" "+Getversionapp());
		} catch (Exception e) {

		}
		final TextView aboutapp =(TextView) findViewById(R.id.aboutappbutton1);
		aboutapp.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				try {
					Intent appemalintent = new Intent(Intent.ACTION_SEND);
					appemalintent.putExtra(Intent.EXTRA_EMAIL,new String[] {getString(R.string.appemail)});
					appemalintent.putExtra(Intent.EXTRA_SUBJECT,getString(R.string.app_p)+": "+getString(R.string.app_name));
					appemalintent.putExtra(Intent.EXTRA_TEXT,"\n"+getString(R.string.appos) + " "+ Build.VERSION.RELEASE+", "+Build.MANUFACTURER+" "+Build.MODEL);
					appemalintent.setType("application/octet-stream");
					startActivity(appemalintent);
				} catch (Exception e) {

				}
			}	
		});
		final TextView appocenca =(TextView) findViewById(R.id.aboutappbutton2);
		appocenca.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				try {
					Intent appocenca = new Intent(Intent.ACTION_VIEW);
					appocenca.setData(Uri.parse(getString(R.string.appocencauri)));
					startActivity(appocenca);
				} catch (Exception e) {
					Intent appocencabr = new Intent(Intent.ACTION_VIEW,Uri.parse(getString(R.string.appocencauribrouse)));
					startActivity(appocencabr);
				}
			}	
		});
		final TextView aboutexit =(TextView) findViewById(R.id.Closeaboutapp);		
		aboutexit.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Aboutapp.super.onDestroy();
				finish();
				Aboutapp.super.onDestroy();
			}	
		});
	} catch (Exception e) {
		// TODO: handle exception
	}
	}

	public String Getversionapp() {
		String versionapp = null;
		try {
			versionapp = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
		} catch (Exception e) {			
		}
		if (versionapp == null) {
			versionapp = getString(R.string.version_app);
		}
		return versionapp;		

	}


	public boolean onCreateOptionsMenu(Menu menu) { 

		getMenuInflater().inflate(R.menu.pmenu, menu);

		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) { 
		if (FlagOnlain == 0) {
			menu.findItem(R.id.loaditem1).setVisible(true);
			menu.findItem(R.id.loaditem1).setEnabled(true);
			} else
			{			
			menu.findItem(R.id.loaditem1).setEnabled(false);
			menu.findItem(R.id.loaditem1).setVisible(false);
			}
			menu.findItem(R.id.pitem2).setEnabled(true);
			menu.findItem(R.id.abitem3).setEnabled(true);
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) { 

		case R.id.loaditem1: 
			GoMain();
			break; 


		case R.id.pitem2:
			//
			setResult(9);
			finish();

			break; 
		case R.id.abitem3: 
			//
			setResult(5);
			finish();

			break;
		}       


		return super.onOptionsItemSelected(item); 

	} 
	private void GoMain() {
		AlertDialog.Builder buildergomain = new AlertDialog.Builder(this);
		buildergomain.setTitle(R.string.lt_app);		
		buildergomain.setMessage(R.string.lmmm_app);		
		buildergomain.setNegativeButton(R.string.qn_app, null);
		buildergomain.setPositiveButton(R.string.qy_app, new OnClickListener() {
			public void onClick(DialogInterface arg0, int arg1) {
				setResult(7);
				finish(); 	            	

			}
		}).create().show();	
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == 7) {
			setResult(7);
			finish();
		}

		if (resultCode ==  5) {
			try {
				openOptionsMenu();
			} catch (Exception e) {

			}
		}

	}

}
